﻿Public Class FormCheckOut
    Dim Time0AM As DateTime = #12:00:00 AM#
    Dim Time8AM As DateTime = #8:00:00 AM#
    Dim Time12PM As DateTime = #12:00:00 PM#
    'Dim Time24P As DateTime =#23:00:00 PM#
    Dim DAReservationDetail As New DSReservationTableAdapters.RESERVATION_DETAILTableAdapter
    Dim DAReservation As New DSReservationTableAdapters.RESERVATIONTableAdapter
    Dim DARoom As New DSFloorManagemantTableAdapters.ROOMSTableAdapter
    Dim DAInvoiceReservation As New DSReservationTableAdapters.View_ReservationInvoiceTableAdapter

    Public PrinterReceipt As String = My.Settings.PrinterName
    Public printDocument As New System.Drawing.Printing.PrintDocument
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
    Public Sub RefreshReservationDetail(ByVal ReservationID As Integer)
        GridReservationDedail.DataSource = DAReservationDetail.SelectReservationByID(ReservationID)
    End Sub
    Private Sub FormCheckOut_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Try
        txtPriceRoom.Select()
        txtPriceRoom.Focus()

        DateOut1.Text = Format(Now, "dd/MM/yyyy")
        RefreshReservationDetail(LblReservation.Text)
        RefreshServicePrice()
    End Sub

    Private Sub RefreshServicePrice()
        Dim PriceHouse As Double = GetRoomPriceByHouse(GetHourse(TimeIn.Value, Timeout.Value), TxtBed.Text)
        txtPriceRoom.Text = GetTotalRoomByDays(GetDay(TimeIn.Value, Timeout.Value), PriceHouse)
        'End Try

        TxtTotalOtherService.Text = GetTotalOtherService()

        TxtExtraPrice.Text = CalExtraRoomPrice(EmptyString(txtPriceRoom.Text), EmptyString(TxtTotalOtherService.Text), EmptyString(TxtPrePaid.Text))
        TxtTotal.Text = TotalService(CDbl(IIf(CDbl(EmptyString(TxtPrePaid.Text)) > CDbl(EmptyString(txtPriceRoom.Text)), EmptyString(TxtPrePaid.Text), EmptyString(txtPriceRoom.Text))))
        TxtSubTotal.Text = TotalService(CDbl(IIf((CDbl(EmptyString(TxtPrePaid.Text)) > CDbl(EmptyString(txtPriceRoom.Text))), EmptyString(TxtPrePaid.Text), EmptyString(txtPriceRoom.Text))))
    End Sub
    Private Function CalExtraRoomPrice(ByVal TotalRoomPrice As Double, ByVal TotalOtherService As Double, ByVal Prepay As Double) As Double

        If (TotalOtherService + TotalRoomPrice) > Prepay Then
            Return 0
        Else
            Return (TotalOtherService + TotalRoomPrice) - Prepay
        End If
    End Function
    Private Function CalculatePriceRoom(ByVal TimeOut As Integer, ByVal Bed As Integer)
        'Case Bed =1 then
        If Bed = 1 Then
            If TimeOut <= 3 Then
                Return 4
            ElseIf TimeOut <= 5 Then
                Return 5
            ElseIf TimeOut <= 7 Then
                Return 12
            Else
                Return 15
            End If
            'Case Bed =2 then
        Else
            If TimeOut <= 3 Then
                Return 8
            ElseIf TimeOut <= 5 Then
                Return 12
                'ElseIf TimeOut <= 7 Then
                '    Return 20
            Else
                Return 15
            End If
        End If

    End Function
    Private Function GetRoomPriceByHouse(ByVal hourse As Integer, ByVal bed As Integer) As Double
        If bed <= 1 Then
            If hourse <= 3 Then
                Return 5
            ElseIf hourse <= 5 Then
                Return 7
            Else
                Return 10
            End If
        Else
            If hourse <= 3 Then
                Return 8
            ElseIf hourse <= 5 Then
                Return 12
            Else
                Return 15
            End If
        End If
    End Function
    Private Function GetTotalRoomByDays(ByVal Daystay As Integer, ByVal Hourse As Integer) As Double
        If Daystay = 0 Then
            Return Hourse
        Else
            Return (Daystay * Hourse) + Hourse

        End If
    End Function
    Private Function TotalService(ByVal ServiceRoom As Double) As Double
        Dim TblService As DataTable = DAReservationDetail.SelectReservationByID(LblReservation.Text)
        Dim Total As Double = 0
        For Each row In TblService.Rows
            Total = Total + CDbl(row("TOTAL_PRICE"))
        Next
        Return Total + ServiceRoom
    End Function
    Private Function GetTotalOtherService() As Double
        Dim TblService As DataTable = DAReservationDetail.SelectReservationByID(LblReservation.Text)
        Dim Total As Double = 0
        For Each row In TblService.Rows
            Total = Total + CDbl(row("TOTAL_PRICE"))
        Next
        Return Total
    End Function
   

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ValidateTextField(txtPriceRoom, "", ErrCheckOut) = False Then Exit Sub
        If CDbl(txtPriceRoom.Text) <= 0 Then
            MessageBox.Show("អ្នកមិនអាច បញ្ចូលតំលៃតូចជាង ឬស្មើ 0បានទេ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        If MessageBox.Show("Do you want save?", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            If DAReservation.CheckOutReservation(Now.Date, Timeout.Value, CDbl(IIf((EmptyString(TxtPrePaid.Text) > EmptyString(txtPriceRoom.Text)), EmptyString(TxtPrePaid.Text), EmptyString(txtPriceRoom.Text))), 2, 1, EmptyString(TxtDiscound.Text), EmptyString(TxtSubTotal.Text), EmptyString(TxtTotal.Text), USER_NAME, GetDay(TimeIn.Value, Timeout.Value), GetHourse(TimeIn.Value, Timeout.Value), EmptyString(TxtExtraPrice.Text), TimeIn.Value, LblReservation.Text) = 1 Then
                'Update Room Status
                DARoom.UpdateRoomStatus(4, lblRoomID.Text)

                'Add More Room Service to Reservation Detail

                DAReservationDetail.InsertNewReservation(LblReservation.Text, 77, "Price stay", 1, CDbl(IIf((EmptyString(TxtPrePaid.Text) > EmptyString(txtPriceRoom.Text)), EmptyString(TxtPrePaid.Text), EmptyString(txtPriceRoom.Text))), CDbl(IIf(EmptyString(TxtPrePaid.Text) > EmptyString(txtPriceRoom.Text), EmptyString(TxtPrePaid.Text), EmptyString(txtPriceRoom.Text))))
                If ChIsInvoice.Checked = True Then
                    Try
                        Dim Invoice As New Invice
                        Dim tblReservation As DataTable = DAInvoiceReservation.GetInvoiceByReservationID(LblReservation.Text)
                        Invoice.SetDataSource(tblReservation)
                        printDocument.PrinterSettings.PrinterName = PrinterReceipt.Trim
                        Invoice.PrintOptions.PrinterName = printDocument.PrinterSettings.PrinterName
                        Invoice.PrintToPrinter(1, False, 1, 1)
                        Invoice.Dispose()
                        Invoice.Close()

                    Catch ex As Exception

                    End Try
                End If
                Me.DialogResult = Windows.Forms.DialogResult.OK
            Else
                MessageBox.Show("Error save Service Check out", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If
    End Sub

    Private Sub TxtDiscound_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtDiscound.KeyPress
        SetDisableKeyString(e)
    End Sub

  
    Private Sub TxtDiscound_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtDiscound.KeyUp
        If CDbl(EmptyString(TxtDiscound.Text)) > CDbl(TxtTotal.Text) Then
            MessageBox.Show("អ្នកមិនបញ្ចុះតំលៃបានទេ ព្រោះ discount ធំជាង", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            TxtDiscound.Text = 0
            TxtSubTotal.Text = CDbl(TxtTotal.Text)
        Else
            TxtSubTotal.Text = CDbl(TxtTotal.Text) - CDbl(EmptyString(TxtDiscound.Text))
        End If

    End Sub

   
    Private Sub txtPriceRoom_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPriceRoom.KeyPress
        SetDisableKeyString(e)
    End Sub

    Private Sub txtPriceRoom_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPriceRoom.KeyUp
        If CDbl(txtPriceRoom.Text) <= 0 Then
            MessageBox.Show("អ្នកមិនអាច បញ្ចូលតំលៃតូចជាង ឬស្មើ 0បានទេ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

            Exit Sub
        End If

        'RefreshServicePrice()
        TxtExtraPrice.Text = CalExtraRoomPrice(EmptyString(txtPriceRoom.Text), EmptyString(TxtTotalOtherService.Text), EmptyString(TxtPrePaid.Text))
        TxtTotal.Text = TotalService(CDbl(IIf((CDbl(EmptyString(TxtPrePaid.Text)) > CDbl(EmptyString(txtPriceRoom.Text))), EmptyString(TxtPrePaid.Text), EmptyString(txtPriceRoom.Text))))
        TxtSubTotal.Text = TotalService(CDbl(IIf(CDbl((EmptyString(TxtPrePaid.Text)) > CDbl(EmptyString(txtPriceRoom.Text))), EmptyString(TxtPrePaid.Text), EmptyString(txtPriceRoom.Text))))

    End Sub
    Private Function CalculatePerod(ByVal d1 As Date, ByVal d2 As Date) As Integer

        Dim ts As TimeSpan = d2.Subtract(d1)
        Dim TotalMinute As Integer = ts.TotalMinutes
        'lblTotalHours.Text = TotalMinute / 60
        'lblTotalDay.Text = CInt(EmptyString(lblTotalHours.Text) / 24)

        LblTotalMinute.Text = ts.Days & " ថ្ងៃ " & ts.Hours & " ម៉ោង " & ts.Minutes & " នាទី " ' & ts.Seconds & " វិនាទី "
        Return ts.Hours
    End Function
    Private Function GetDay(ByVal d1 As Date, ByVal d2 As Date) As Integer
        Dim ts As TimeSpan = d2.Subtract(d1)
        Return ts.Days
    End Function
    Private Function GetHourse(ByVal d1 As Date, ByVal d2 As Date) As Integer
        Dim ts As TimeSpan = d2.Subtract(d1)
        Return ts.Hours
    End Function
    Private Function GetMinute(ByVal d1 As Date, ByVal d2 As Date) As Integer
        Dim ts As TimeSpan = d2.Subtract(d1)
        Return ts.Minutes
    End Function
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim d1 As Date = TimeIn.Value
        Dim d2 As Date = Timeout.Value

        Dim ts As TimeSpan = d2.Subtract(d1)
        MsgBox(CInt(ts.TotalDays) & " : " & CInt(ts.TotalHours) & " : " & CInt(ts.TotalMinutes))
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Timeout.Value = Now

        CalculatePerod(TimeIn.Value, Timeout.Value)
    End Sub

    Private Sub Timeout_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Timeout.KeyUp
        RefreshServicePrice()
    End Sub

    Private Sub TimeIn_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TimeIn.KeyUp
        RefreshServicePrice()
    End Sub
End Class